//
//  FMDBViewController.m
//  HappyBug
//
//  Created by liulei on 2018/12/11.
//  Copyright © 2018年 www.hc.com.cn. All rights reserved.
//

#import "FMDBViewController.h"
#import "StudentFMDNManager.h"
#import "Student.h"



@interface FMDBViewController ()

@end

@implementation FMDBViewController{
    
    NSArray *itemArrays;
    NSString *selectText;
    NSString *nameTxt;
    NSString *idTxt;
    UITableView *tableview;
    NSArray *StudentArrays;
    
    BOOL  queryFlag;

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorWithRed:196 green:184 blue:184 alpha:0.8]];
    
    //
    queryFlag=NO;
    [self querystu];
    
    itemArrays=@[@"男",@"女"];
    UITextField *addNameField =[[UITextField alloc]initWithFrame:CGRectMake(100, 50, [UIScreen mainScreen].bounds.size.width-200, 40)];
    addNameField.textAlignment=NSTextAlignmentCenter;
    [addNameField setPlaceholder:@"输入姓名:"];
    addNameField.tintColor = [UIColor redColor];
    addNameField.delegate=self;
    addNameField.tag=222;
    [addNameField setBackgroundColor:[UIColor colorWithRed:133 green:133 blue:133 alpha:0.6]];
    [self.view addSubview:addNameField];
    
    
    
  
    
    
    
    
    UITextField *sexNameField =[[UITextField alloc]initWithFrame:CGRectMake(100, addNameField.frame.origin.x, [UIScreen mainScreen].bounds.size.width-200, 40)];
    sexNameField.delegate=self;
    sexNameField.textAlignment=NSTextAlignmentCenter;
    sexNameField.tag=111;
    sexNameField.keyboardType=UIKeyboardTypeNumberPad;
    [sexNameField setPlaceholder:@"输入ID:"];
    [sexNameField setBackgroundColor:[UIColor colorWithRed:133 green:133 blue:133 alpha:0.6]];
    [self.view addSubview:sexNameField];

    
    
    
    UIPickerView *sexPicker=[[UIPickerView alloc]initWithFrame:CGRectMake(50, sexNameField.frame.origin.y+sexNameField.frame.size.height+10, [UIScreen mainScreen].bounds.size.width-100,80)];
    sexPicker.tintColor=[UIColor redColor];
    sexPicker.dataSource=self;
    sexPicker.delegate=self;
    [self.view addSubview:sexPicker];
    
    
    
    //添加
    UIButton *addbutton=[[UIButton alloc]initWithFrame:CGRectMake(20,[UIScreen mainScreen].bounds.size.height-80,80, 34)];
    [addbutton setTitle:@"add" forState:UIControlStateNormal];
    [addbutton setBackgroundColor:[UIColor redColor]];
    addbutton.layer.cornerRadius=5;
    [addbutton addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addbutton];
    
    
    //删除
    UIButton *delbutton=[[UIButton alloc]initWithFrame:CGRectMake(addbutton.frame.origin.x+addbutton.frame.size.width+10, [UIScreen mainScreen].bounds.size.height-80,80, 34)];
    [delbutton setTitle:@"删除" forState:UIControlStateNormal];
    [delbutton setBackgroundColor:[UIColor redColor]];
    delbutton.layer.cornerRadius=5;
    [delbutton addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:delbutton];
    
    //修改
    
    UIButton *gaibutton=[[UIButton alloc]initWithFrame:CGRectMake(delbutton.frame.origin.x+delbutton.frame.size.width+10, [UIScreen mainScreen].bounds.size.height-80,80, 34)];
    [gaibutton setTitle:@"修改" forState:UIControlStateNormal];
    [gaibutton setBackgroundColor:[UIColor redColor]];
    gaibutton.layer.cornerRadius=5;
    [gaibutton addTarget:self action:@selector(update:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:gaibutton];
    
    
    //查询
    UIButton *querybutton=[[UIButton alloc]initWithFrame:CGRectMake(gaibutton.frame.origin.x+gaibutton.frame.size.width+10, [UIScreen mainScreen].bounds.size.height-80,80, 34)];
    [querybutton setTitle:@"查询" forState:UIControlStateNormal];
    [querybutton setBackgroundColor:[UIColor redColor]];
    querybutton.layer.cornerRadius=5;
    [querybutton addTarget:self action:@selector(querystu) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:querybutton];
    
    
    //UITableView
    
    tableview =[[UITableView alloc]initWithFrame:CGRectMake(0, sexPicker.frame.origin.y+sexPicker.frame.size.height+10, [UIScreen mainScreen].bounds.size.width,320) style:UITableViewStylePlain];
    tableview.delegate=self;
    tableview.dataSource=self;
    //[tableview setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:tableview];
}


- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath {
    
    NSString *ID =@"tablecell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if(!cell){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];;
    }
    Student *stu =StudentArrays[indexPath.row];
    cell.textLabel.text=[NSString stringWithFormat:@"Student:ID:%@,Name:%@,Sex:%@",stu.stuCode,stu.name,stu.sex];
    return cell;
}
//返回每个分区的行数
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [StudentArrays count];
}
//输入内容
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
        if(textField.tag==111){
            
            idTxt=textField.text;
            
            
        }else if (textField.tag==222){
            nameTxt=textField.text;
            
        }
    return YES;
}
// 返回多少列 textFieldShouldEndEditing
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1 ;
}

// 返回每列的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    return [itemArrays count];
}

// 返回每行的标题
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return itemArrays[row];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 50;
}


// 选中行显示在label上
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel) {
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30);
        pickerLabel.font = [UIFont systemFontOfSize:15];
        pickerLabel.contentMode = UIViewContentModeCenter;
        pickerLabel.textAlignment=NSTextAlignmentCenter;
        pickerLabel.textColor = [UIColor blueColor];
    }
    //选中的内容
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    selectText=pickerLabel.text;
    NSLog(@"selectText:%@",selectText);
    return pickerLabel;
}


- (void)add:(id)sender {
    
    if(idTxt ==nil||nameTxt ==nil){
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"输入内容不能为空"]  delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [alert show];
        
        return;
    }
    
    Student *stu = [Student studentWith:idTxt name:nameTxt sex:selectText];
    
    BOOL isInsert = [StudentFMDNManager addStudent:stu];
    
    if(isInsert){
        
        [self querystu];
        
        
    }else{
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"添加失败"]  delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [alert show];
    }
    
}
- (void)delete:(id)sender {
    
    //delete from t_STUDENT_TABLE where stuCode=%@
    NSString *sql = [NSString stringWithFormat:@"delete from t_STUDENT_TABLE "];
    
    BOOL   b =  [StudentFMDNManager deleteStudent:sql];
    
    if(b){
        
        [self querystu];
        
    }else{
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"删除失败"]  delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [alert show];
    }
    
    
}

- (void)update:(id)sender {
    
    NSString *sql = [NSString stringWithFormat:@"update t_STUDENT_TABLE set name='%@' , sex='%@' where stuCode = %@",nameTxt,selectText,idTxt];
    
    BOOL b=  [StudentFMDNManager updateStudent:sql];
    
    if(b){
        
        [self querystu];

        
    }else{
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"修改失败"]  delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [alert show];
    }
}

- (void)querystu{
    queryFlag=YES;
    NSString *stumsg=nil;
    StudentArrays = [StudentFMDNManager queryStudent:nil];
    if([StudentArrays count] !=0){
        for(int i=0;i<[StudentArrays count];i++){
            Student *stu=StudentArrays[i];
            NSString *code=  stu.stuCode;
            NSString *sex=stu.sex;
            NSString *name= stu.name;
            stumsg=[NSString stringWithFormat:@"学生编号:%@,sex:%@,name:%@",code,sex,name];
            [tableview reloadData];
        }
        
    }else{
        
        [tableview reloadData];

    }
    
}




- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
    NSLog(@"立即发送通知");
}

@end
